﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;

namespace SalaryChannel
{
    public delegate void Action();   
     public partial class MessageBox : UserControl
    {
         private Action action;
        public MessageBox()
        {
            InitializeComponent();
            this.KeyDown += new KeyEventHandler(MessageBox_KeyDown);
        }

        void MessageBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                OKButton_Click(null, null);
                e.Handled = true;
            }
            else if (e.Key == Key.Escape)
            {
                cancelButton_Click(null, null);
                e.Handled = true;
            }
        }

       
        /// <summary>
        /// Show the message
        /// </summary>
        /// <param name="message"></param>
        public void ShowDialog(string message,Title title)
        {
            IsEnabled = true;
            switch (title)
            { 
                case Title.Error:
                    titleText.Text = Properties.Resource.Error;
                    break;

                case Title.Warning:
                    titleText.Text = Properties.Resource.Warining;
                    break;

                case Title.Information:
                    titleText.Text = Properties.Resource.Infor;
                    break;
            }
            
            messageText.Text = message;
            cancel.Visibility = Visibility.Collapsed;
            Visibility = Visibility.Visible;

            this.Focus();
        }

        public void ShowDialog(string message, Action completedAction, Title title)
        {
            IsEnabled = true;
            switch (title)
            {
                case Title.Error:
                    titleText.Text = Properties.Resource.Error;
                    break;

                case Title.Warning:
                    titleText.Text = Properties.Resource.Warining;
                    break;

                case Title.Information:
                    titleText.Text = Properties.Resource.Infor;
                    break;
            }

            messageText.Text = message;
            cancel.Visibility = Visibility.Visible;
            Visibility = Visibility.Visible;
            this.action = completedAction;
            this.Focus();
        }
        /// <summary>
        /// Do the action and disappear 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>       
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            IsEnabled = false;
            this.Visibility = Visibility.Collapsed;
            if (action != null)
            {
                action();
                this.action = null;
            }
        }

        /// <summary>
        /// Disappear with doing nothing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>        
        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            IsEnabled = false;
            action = null;
            this.Visibility = Visibility.Collapsed;
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Width < 845.0 || e.NewSize.Height < 600.0)
            {
                // don't shrink
                rootScale.ScaleX = 1.0;
                rootScale.ScaleY = 1.0;
            }
            else
            {
                // resize keeping aspect ratio the same

                if (e.NewSize.Width / e.NewSize.Height > 845.0 / 600.0)
                {
                    // height is our constraining property
                    rootScale.ScaleY = e.NewSize.Height / 600.0;
                    rootScale.ScaleX = rootScale.ScaleY;
                }
                else
                {
                    // either width is our constraining property, or the user
                    // managed to nail our aspect ratio perfectly.
                    rootScale.ScaleX = e.NewSize.Width / 845.0;
                    rootScale.ScaleY = rootScale.ScaleX;
                }
            }
        }

        public void ShowDialog(string p)
        {
            this.ShowDialog(p, Title.Information);
            this.Focus();
        }
    }
     public enum Title
     {
         Warning,
         Error,
         Information
     }
     public enum DialogResult
     {
         OK,
         Cancel
     }
}
